package com.tanhua.server.exception;

import com.tanhua.model.vo.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @Author Pan Rundong
 * @Date 2023-02-25 20:22
 * @Desc 全局异常处理器
 */
@ControllerAdvice
public class ExceptionAdvice {
    /**
     * 处理业务异常
     * @Author: Pan Rundong
     * @param businessException
     * @return org.springframework.http.ResponseEntity
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity solveBusiness(BusinessException businessException) {
        ErrorResult errorResult = businessException.getErrorResult();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 处理全局异常
     * @Author: Pan Rundong
     * @param ex
     * @return org.springframework.http.ResponseEntity
     */
    @ExceptionHandler(Exception.class)
    public ResponseEntity solveUnKnown(Exception ex) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResult.error());
    }
}
